﻿//创建时间: 2010-10-18
//创建作者: 李 城
//功能说明: 系统参数类型 添加 修改  参数管理
//最后修改: 2010-10-18
using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using TRPOP.Common;
using TRPOP.CMS.Web.UI;


using TRPOP.CMS.Business;
using TRPOP.CMS.Model;

using System.Data;
using System.Text;
using System.Collections;
using Newtonsoft.Json;

namespace TRPOP.CMS.Web.Admin
{
    public partial class Param_manage : BaseAdmin
    {
        #region 当前页面权限验证
        protected override void SubPageInit()
        {
            base.m_SubPagePermission = Plugin.PermissionKeys.PermissionEnum.SystemManage_SysParam;
        }
        #endregion


        #region 页面变量
        protected int m_cid = 0;
        ParamTypeManage oParamTypeManage = new ParamTypeManage();
        protected string m_linkStr = "";
        private string m_varPre = "{syssetting.";
        private string m_varAppend = "}";
        #endregion

        #region 页面事件

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadBase();
            }
            LoadAllLink();
        }
        //批量修改
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (txtName.Text.Trim() != ((ParamTypeInfo)GetPageViewState("ParamTypeInfo")).Name)
            {
                ParamTypeInfo info = (ParamTypeInfo)GetPageViewState("ParamTypeInfo");
                info.Name = txtName.Text.Trim();
                oParamTypeManage.Save(info);
                InitPageViewState("ParamTypeInfo", info);
            }
            foreach (string tempelement in Request.Form.AllKeys)
            {
                if (tempelement.IndexOf("_VarValue") > 0)
                {
                    ArrayList ArrayValue = new ArrayList();
                    ArrayValue.Add(tempelement.Replace("_VarValue", ""));
                    ArrayValue.Add(Request.Form[tempelement.Replace("_VarValue", "_VarName")].ToString());
                    ArrayValue.Add(Request.Form[tempelement].ToString());
                    SYSConfig.SaveVarValue(Config.VarAray.GetVarAttribute(), ArrayValue, ((ParamTypeInfo)GetPageViewState("ParamTypeInfo")).KeyWord);
                }
            }
            base.SaveLog(Plugin.LogKeys.OperObject.TemplateOper, Plugin.LogKeys.OperResult.EditSubmit);
            SubmitAlertAndThisNewLocation(EnumSubmitResult.Success, "Param_manage.aspx?cid=" + ((ParamTypeInfo)GetPageViewState("ParamTypeInfo")).ParamTypeId.ToString());
        }
        //参数类型 新增
        protected void btnpithyType_Click(object sender, EventArgs e)
        {
            string strMSG = "类型名或关键字";
            if (!PageAssist.CheckForm(txtTypeName, ref strMSG) || !PageAssist.CheckForm(txtTypeKeyWords, ref strMSG))
            {
                Alert(strMSG);
                return;
            }
            ParamTypeInfo oParamTypeInfo = new ParamTypeInfo();
            oParamTypeInfo.IsSysParam = true;
            oParamTypeInfo.KeyWord = txtTypeKeyWords.Text.Trim();
            oParamTypeInfo.Name = txtTypeName.Text.Trim();
            base.SaveLog(Plugin.LogKeys.OperObject.TemplateOper, Plugin.LogKeys.OperResult.AddParam);
            SubmitAlertAndThisNewLocation(oParamTypeManage.Save(oParamTypeInfo), "Param_manage.aspx?cid=" + oParamTypeInfo.ParamTypeId.ToString());
        }
        //参数 新增
        protected void btnpithyParam_Click(object sender, EventArgs e)
        {
            string strMSG = "参数名 、关键字或参数值";
            if (!PageAssist.CheckForm(txtVarName, ref strMSG) ||
                !PageAssist.CheckForm(txtVarKey, ref strMSG) ||
                !PageAssist.CheckForm(txtVarValue, ref strMSG)
                )
            {
                Alert(strMSG);
                return;
            }
            ParamTypeInfo oParamTypeInfo = (new ParamTypeManage()).GetInfoById(ddlType.SelectedValue.ToInt());
            ArrayList ArrayValue = new ArrayList();
            ArrayValue = SYSConfig.GetVarValue(txtVarKey.Text.Trim(), oParamTypeInfo.KeyWord);
            if (ArrayValue != null)
            {
                Alert("操作出错，要添加的参数“" + txtVarKey.Text.Trim() + "”已经存在。");
                return;
            }
            else
                ArrayValue = new ArrayList();
            ArrayValue.Add(txtVarKey.Text);
            ArrayValue.Add(txtVarName.Text);
            ArrayValue.Add(txtVarValue.Text);
            ArrayValue.Add(rbtValType.SelectedValue);

            SubmitAlertAndThisNewLocation(SYSConfig.SaveVarValue(Config.VarAray.GetVarAttribute(), ArrayValue, oParamTypeInfo.KeyWord), "Param_manage.aspx?cid=" + oParamTypeInfo.ParamTypeId.ToString());
        }

        #endregion

        #region 页面方法


        public void LoadBase()
        {
            DelParam();//如果是删除操作，则执行
            int.TryParse(InitRequestViewState("cid"), out m_cid);
            //初始化textbox等的数据
            if (m_cid == 0)
            {
                m_cid = 1;
                InitPageViewState("cid", "1");
            }
            ParamTypeInfo oParamTypeInfo = oParamTypeManage.GetInfoById(m_cid);
            InitPageViewState("ParamTypeInfo", oParamTypeInfo);
            if (GetPageViewState("ParamTypeInfo") != null)
            {
                txtName.Text = ((ParamTypeInfo)GetPageViewState("ParamTypeInfo")).Name;
                LoadFormItem((ParamTypeInfo)GetPageViewState("ParamTypeInfo"));
            }
            else
            {
                Alert("请通过正确的路径访问页面");
                return;
            }
            ddlType.DataSource = oParamTypeManage.GetListByCondition(0, "", "ParamTypeId");
            ddlType.DataTextField = "Name";
            ddlType.DataValueField = "ParamTypeId";
            ddlType.DataBind();

            rbtValType.DataSource = PageAssist.GetAllVarInputType();
            rbtValType.DataTextField = "Key";
            rbtValType.DataValueField = "Value";
            rbtValType.DataBind();
            rbtValType.SelectedIndex = 0;
        }

        //加载html元素
        public void LoadFormItem(ParamTypeInfo oParamTypeInfo)
        {
            string strReg = ltFormItem.Text;
            StringBuilder strb = new StringBuilder();
            string ovarType = "text";
            if (oParamTypeInfo != null)
            {
                DataTable tableTemp = SYSConfig.GetDataSource(oParamTypeInfo.KeyWord, "//items");
                ovarType = "text";
                if (tableTemp != null && tableTemp.Rows.Count > 0)
                {
                    foreach (DataRow TempRow in tableTemp.Rows)
                    {
                        try
                        {
                            if (TempRow["VarType"].ToString() != "")
                            {
                                ovarType = TempRow["VarType"].ToString();
                            }
                        }
                        catch
                        {

                        }
                        strb.Append(creatOneItem(ovarType, TempRow, oParamTypeInfo));
                        strb.Append(System.Environment.NewLine);
                    }
                    ltFormItem.Text = strb.ToString();
                }
                else
                    ltFormItem.Text = "";
            }
            else
                ltFormItem.Text = "";

        }

        //加载一个html元素
        public string creatOneItem(string ovarType, DataRow TempRow, ParamTypeInfo oParamTypeInfo)
        {
            string returnstr = "";
            string varName = m_varPre + TempRow["VarKey"].ToString() + m_varAppend;
            switch (ovarType)
            {
                case "textarea":
                    returnstr = string.Format(ltFormItem.Text,
                         TempRow["VarName"].ToString() + FormAssist.Addhidden(TempRow["VarKey"].ToString() + "_VarName", TempRow["VarKey"].ToString() + "_VarName", TempRow["VarName"].ToString()),
                         FormAssist.Addtextarea(TempRow["VarKey"].ToString() + "_VarValue",
                                                TempRow["VarKey"].ToString() + "_Id",
                                                4,
                                                20,
                                                TempRow["VarValue"].ToString(), null),
                         varName,
                         "<a  href=\"javascript:show_dialog('Param_save.aspx?cid=" + oParamTypeInfo.ParamTypeId.ToString() + "&VarKey=" + TempRow["VarKey"].ToString() + "',500,280);\">高级</a>" +
                             "&nbsp;|&nbsp;<a  href=\"Param_manage.aspx?cid=" + oParamTypeInfo.ParamTypeId.ToString() + "&VarKey=" + TempRow["VarKey"].ToString().Trim() + " &action=del\" onclick=\"return confirm('你确定要删除变量吗？');\">删除</a>"
                        );
                    break;
                case "radio":
                    List<JavaScriptObject> lst = new List<JavaScriptObject>();
                    JavaScriptObject ojsonobjTrue = new JavaScriptObject();
                    ojsonobjTrue.Add(HtmlAttributeKey.RadioShowName, "是");
                    ojsonobjTrue.Add(HtmlAttributeKey.RadioValue, "true");
                    JavaScriptObject ojsonobjFalse = new JavaScriptObject();
                    ojsonobjFalse.Add(HtmlAttributeKey.RadioShowName, "否");
                    ojsonobjFalse.Add(HtmlAttributeKey.RadioValue, "false");
                    if (TempRow["VarValue"].ToString().Trim().ToLower() == "true")
                    {
                        ojsonobjTrue.Add(HtmlAttributeKey.Radiochecked, "true");
                    }
                    else
                        ojsonobjFalse.Add(HtmlAttributeKey.Radiochecked, "true");
                    lst.Add(ojsonobjTrue);
                    lst.Add(ojsonobjFalse);
                    returnstr = string.Format(ltFormItem.Text,
                         TempRow["VarName"].ToString() + FormAssist.Addhidden(TempRow["VarKey"].ToString() + "_VarName", TempRow["VarKey"].ToString() + "_VarName", TempRow["VarName"].ToString()),
                         FormAssist.Addradion(TempRow["VarKey"].ToString() + "_VarValue", lst),
                         varName,
                         "<a  href=\"javascript:show_dialog('Param_save.aspx?cid=" + oParamTypeInfo.ParamTypeId.ToString() + "&VarKey=" + TempRow["VarKey"].ToString() + "',500,280);\">高级</a>" +
                             "&nbsp;|&nbsp;<a  href=\"Param_manage.aspx?cid=" + oParamTypeInfo.ParamTypeId.ToString() + "&VarKey=" + TempRow["VarKey"].ToString().Trim() + " &action=del\" onclick=\"return confirm('你确定要删除变量吗？');\">删除</a>"
                        );
                    break;
                default:
                    returnstr = string.Format(ltFormItem.Text,
                         TempRow["VarName"].ToString() + FormAssist.Addhidden(TempRow["VarKey"].ToString() + "_VarName", TempRow["VarKey"].ToString() + "_VarName", TempRow["VarName"].ToString()),
                         FormAssist.Addtext(TempRow["VarKey"].ToString() + "_VarValue", TempRow["VarKey"].ToString() + "_Id", TempRow["VarValue"].ToString(), null),
                         varName,
                         "<a  href=\"javascript:show_dialog('Param_save.aspx?cid=" + oParamTypeInfo.ParamTypeId.ToString() + "&VarKey=" + TempRow["VarKey"].ToString() + "',500,280);\">高级</a>" +
                           "&nbsp;|&nbsp;<a  href=\"Param_manage.aspx?cid=" + oParamTypeInfo.ParamTypeId.ToString() + "&VarKey=" + TempRow["VarKey"].ToString().Trim() + " &action=del\" onclick=\"return confirm('你确定要删除变量吗？');\">删除</a>"
                        );
                    break;
            }
            return returnstr;
        }

        //加载所有参数类型的的链接页面
        public void LoadAllLink()
        {
            //初始化快捷链接
            foreach (ParamTypeInfo TempInfo in oParamTypeManage.GetListByCondition(0, "", "ParamTypeId"))
            {
                m_linkStr += " | " + "<a href=\"Param_manage.aspx?cid=" + TempInfo.ParamTypeId.ToString() + "\" >" + TempInfo.Name + "</a>";
            }
            m_linkStr += " | ";
        }

        //删除参数
        public void DelParam()
        {
            string action = RequestHelper.RequestString("action");
            if (action.IsEmpty())
                return;
            switch (action)
            {
                case "del":
                    ArrayList oArrayKey = new ArrayList();
                    oArrayKey.Add(RequestHelper.RequestString("VarKey"));
                    ParamTypeInfo oParamTypeInfo = new ParamTypeInfo();
                    oParamTypeInfo = oParamTypeManage.GetInfoById(RequestHelper.RequestInt("cid"));
                    SubmitAlert(SYSConfig.DelConfigValue(oArrayKey, oParamTypeInfo.KeyWord));
                    break;
                default:
                    break;
            }

        }

        #endregion
    }
}
